import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import ElementPlus from 'element-plus';
import 'element-plus/lib/theme-chalk/index.css';
import axios from 'axios';
import { ElMessage } from 'element-plus';

//避免跨域时，后端session.getAttribute方法取到的值为null
axios.defaults.withCredentials = true;

// 访问控制
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    if (store.state.userInfo.isLogin) {
      next(false);
      ElMessage({
        showClose: true,
        message: '您已经登录，请注销后再登录！',
        type: 'error',
        offset: 200,
      });
    } else {
      next();
    }
  } else if (to.path === '/register') {
    if (store.state.userInfo.isLogin) {
      next(false);
      ElMessage({
        showClose: true,
        message: '您已经登录，请注销后再注册！',
        type: 'error',
        offset: 200,
      });
    } else {
      next();
    }
  } else if (to.path === '/findPassword') {
    if (store.state.userInfo.isLogin) {
      ElMessage({
        showClose: true,
        message: '您已经登录，请注销后再访问改页面！',
        type: 'error',
        offset: 200,
      });
      next(false);
    } else {
      next();
    }
  } else if (to.path === '/buyTicket') {
    if (from.path === '/booking' && store.state.userInfo.isLogin) {
      next();
    } else {
      // '/'代表通过直接打网址或者刷新进入
      if (from.path === '/') {
        router.push('main');
        next(false);
      } else {
        next(false);
      }
    }
  } else {
    next();
  }
})

createApp(App)
  .use(store)
  .use(router)
  .use(ElementPlus)
  .mount('#app');
axios.defaults.withCredentials = true;